#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(800, 600);

    QAction *openAction = new QAction(QIcon(":/res/open.ico"), "打开", this);
    openAction->setShortcut(tr("Ctrl+O"));
    openAction->setStatusTip("打开文件");
    connect(openAction, &QAction::triggered, this, &MainWindow::OpenFile);

    QAction *saveAction = new QAction(QIcon(":/res/save"), "保存", this);
    saveAction->setShortcut(tr("Ctrl+S"));
    saveAction->setStatusTip("保存文件");
    connect(saveAction, &QAction::triggered, this, &MainWindow::SaveFile);

    QAction *quitAction = new QAction(QIcon(":/res/close"), "退出", this);
    quitAction->setShortcut(tr("Ctrl+Q"));
    quitAction->setStatusTip(tr("退出程序"));
    connect(quitAction, &QAction::triggered, this, &MainWindow::CloseFile);

    QAction *zoomInAction = new QAction(QIcon(":/res/zoom_in"), "放大", this);
    zoomInAction->setStatusTip("放大");
    connect(zoomInAction, &QAction::triggered, this, &MainWindow::ZoomIn);

    QAction *zoomOutAction = new QAction(QIcon(":/res/zoom_out"), "缩小", this);
    zoomOutAction->setStatusTip("缩小");
    connect(zoomOutAction, &QAction::triggered, this, &MainWindow::ZoomOut);

    QAction *resetAction = new QAction(QIcon(":/res/zoom_reset"), "重置", this);
    resetAction->setStatusTip("重置大小");
    connect(resetAction, &QAction::triggered, this, &MainWindow::ZoomReset);

    QAction *previousAction = new QAction(QIcon(":/res/previouspage"), "上一页", this);
    previousAction->setStatusTip("上一页");
    connect(previousAction, &QAction::triggered, this, &MainWindow::PreviousPage);

    lineEdit = new QLineEdit(this);
    lineEdit->setFixedWidth(30);
    lineEdit->setText("0");
    connect(lineEdit, &QLineEdit::returnPressed, this, &MainWindow::GotoPage);

    QAction *NextAction = new QAction(QIcon(":/res/nextpage"), "下一页", this);
    NextAction->setStatusTip("下一页");
    connect(NextAction, &QAction::triggered, this, &MainWindow::NextPage);

    //第一种添加menu方法
    QMenu *menu = menuBar()->addMenu(tr("文件(F)"));
    menu->addAction(openAction);
    menu->addAction(saveAction);
    menu->addSeparator();
    menu->addAction(quitAction);

    //第二种添加menu方法
    QMenu *editMenu = new QMenu("编辑(E)");
    menuBar()->addMenu(editMenu);
    editMenu->addAction(openAction);
    editMenu->addAction(saveAction);
    editMenu->addSeparator();
    editMenu->addAction(quitAction);

    QToolBar *openTool = new QToolBar(this);
    addToolBar(Qt::TopToolBarArea, openTool);
    openTool->addAction(openAction);
    openTool->addAction(saveAction);
    openTool->addSeparator();
    openTool->addAction(quitAction);
    openTool->addSeparator();
    openTool->addAction(zoomInAction);
    openTool->addAction(resetAction);
    openTool->addAction(zoomOutAction);
    openTool->addSeparator();
    openTool->addAction(previousAction);
    openTool->addWidget(lineEdit);
    openTool->addAction(NextAction);

    mainWindow = new QMdiArea();
    setCentralWidget(mainWindow);
    mainWindow->setViewMode(QMdiArea::TabbedView);//带toolBar
    connect(mainWindow, &QMdiArea::subWindowActivated, this, &MainWindow::UpdataPageNum);
}

MainWindow::~MainWindow()
{

}


void MainWindow::OpenFile()
{
    QString strFilter = "*.pdf";
    QString strDir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    path = QFileDialog::getOpenFileName(this,"PDF阅读器", strDir, strFilter);

    if(path.isEmpty())
        return;

    QFileInfo fileInfo = QFileInfo(path);

    QString file = fileInfo.fileName();
    if(file.isEmpty())
    {
        qDebug() << "file is empty";
        return ;
    }

    ChildWindow *child = new ChildWindow(path, this);
    child->setWindowTitle(file);
    connect(child, &ChildWindow::page_change, this, &MainWindow::UpdataPageNum);
    mainWindow->addSubWindow(child);

/*

    qDebug() << fileInfo.path();
    qDebug() << file;

    NewFile();
    RefreshWindow();
*/
}

void MainWindow::SaveFile()
{

}

void MainWindow::CloseFile()
{
    mainWindow->closeActiveSubWindow();
}

void MainWindow::ZoomIn()
{
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
        return ;


    child->changeImageSize(1);
    refreshWidget();
}

void MainWindow::ZoomOut()
{
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
        return ;

    child->changeImageSize(-1);
    refreshWidget();
}

void MainWindow::ZoomReset()
{
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
        return ;

    child->changeImageSize(0);
    refreshWidget();
}

void MainWindow::PreviousPage()
{
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
        return ;

    int page = child->getCurrentPage();
    if(page<=0)
        return ;
    lineEdit->setText(QString::number(page-1));
    GotoPage();
}

void MainWindow::NextPage()
{
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
        return ;

    int page = child->getCurrentPage();
    lineEdit->setText(QString::number(page+1));
    GotoPage();
}

void MainWindow::UpdataPageNum()
{
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
    {
        return ;
    }
    int page = child->getCurrentPage();
    lineEdit->setText(QString::number(page));
}

ChildWindow *MainWindow::getCurrentChildWindow()
{
    QMdiSubWindow *childSubWindow = mainWindow->activeSubWindow();
    if(childSubWindow == nullptr)
    {
        return nullptr;
    }

    ChildWindow *child = static_cast<ChildWindow *>(childSubWindow->widget());
    if(child == nullptr)
    {
        return nullptr;
    }
    return child;
}

void MainWindow::GotoPage()
{
    QString strPage = lineEdit->text();
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
        return ;
    child->gotoPage(strPage.toInt());
}

void MainWindow::refreshWidget()
{
    ChildWindow *child = getCurrentChildWindow();
    if(child == nullptr)
        return ;

    child->refreshWidget();
}


